package com.webcamtracker.image.color.hsv;

import com.webcamtracker.image.Color;
import com.webcamtracker.utils.MathFunctions;

public class HSVColor implements Color {

    private double hue;
    private double saturation;
    private double brightness;

    public HSVColor(double hue, double saturation, double brightness) {
        this.hue = hue;
        this.brightness = brightness;
        this.saturation = saturation;
    }

    public HSVColor(float[] hsbValues) {
        this.hue = hsbValues[0];
        this.saturation = hsbValues[1];
        this.brightness = hsbValues[2];
    }

    public double getHue() {
        return hue;
    }

    public void setHue(double hue) {
        this.hue = hue;
    }

    public double getBrightness() {
        return brightness;
    }

    public void setBrightness(double brightness) {
        this.brightness = brightness;
    }

    public double getSaturation() {
        return saturation;
    }

    public void setSaturation(double saturation) {
        this.saturation = saturation;
    }


    public String toString() {
        return "HSVColor{" +
                "h=" + hue +
                ", s=" + saturation +
                ", b=" + brightness +
                '}';
    }

    public boolean isSimilar(Color searchColor, double threadsHold) {
        double distance = getEuclideanDistance(searchColor);
        return distance <= threadsHold / 100;
    }

    public double[] elements() {
        return new double[]{getHue(), getSaturation(), getBrightness()};
    }

    public double getEuclideanDistance(Color searchColor) {
        double colorMatrix[] = new double[]{getHue()/*,getSaturation()*//*, getBrightness()*/};
        double searchColorMatrix[] = new double[]{((HSVColor) searchColor).getHue()/*,((HSVColor)searchColor).getSaturation()*//*,((HSVColor)searchColor).getBrightness()*/};
        return MathFunctions.euclideanDistance(colorMatrix, searchColorMatrix);
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        HSVColor hsvColor = (HSVColor) o;

        return Double.compare(hsvColor.brightness, brightness) == 0 &&
                Double.compare(hsvColor.hue, hue) == 0 &&
                Double.compare(hsvColor.saturation, saturation) == 0;
    }

    public int hashCode() {
        int result;
        long temp;
        temp = hue != +0.0d ? Double.doubleToLongBits(hue) : 0L;
        result = (int) (temp ^ (temp >>> 32));
        temp = saturation != +0.0d ? Double.doubleToLongBits(saturation) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = brightness != +0.0d ? Double.doubleToLongBits(brightness) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}
